# import pandas as pd
# import webbrowser
# from pyecharts.charts import Radar
# from pyecharts import options as opts
#
# # === 步骤 1：读取并整合 2020-2025 各年污染物数据 ===
#
# file_path = "data/北京空气质量_2020到2025.xlsx"  # 修改为你的实际路径
# sheet_names = ['2020', '2021', '2022', '2023', '2024', '2025']
# pollutants = ['PM2.5', 'PM10', 'So2', 'No2', 'Co', 'O3']
#
# combined_data = []
#
# for year in sheet_names:
#     df_year = pd.read_excel(file_path, sheet_name=year)
#     df_year['year'] = int(year)
#     df_year = df_year[pollutants + ['year']]
#     combined_data.append(df_year)
#
# # 合并所有年份数据
# df_all = pd.concat(combined_data, ignore_index=True)
#
# # 计算每年各污染物的年均值
# yearly_avg = df_all.groupby('year')[pollutants].mean().reset_index()
# year_labels = yearly_avg['year'].astype(str).tolist()
#
# # === 步骤 2：绘制雷达图 ===
#
# # 设置每个指标的最大值为数据中最大值的1.2倍
# schema = [
#     opts.RadarIndicatorItem(name=p, max_=max(df_all[p]) * 1.2)
#     for p in pollutants
# ]
#
# # 每年的污染物均值列表
# radar_data = [yearly_avg[pollutants].iloc[i].tolist() for i in range(len(yearly_avg))]
#
# # 创建雷达图对象
# radar = (
#     Radar()
#     # .add_schema(schema=schema, shape='polygon')
#     .add_schema(
#         schema=schema,
#         shape="polygon",
#         radius="70%",  # 控制图形整体大小
#         textstyle_opts=opts.TextStyleOpts(font_size=12)
#     )
#
#     .set_global_opts(
#         title_opts=opts.TitleOpts(title="2020–2025年 北京主要污染物年均浓度雷达图", pos_left='center'),
#         legend_opts=opts.LegendOpts(pos_bottom='5%', type_='scroll'),
#         toolbox_opts=opts.ToolboxOpts()
#     )
# )
#
# # 添加每一年的多边形曲线
# for i, year in enumerate(year_labels):
#     radar.add(
#         series_name=year,
#         data=[radar_data[i]],
#         linestyle_opts=opts.LineStyleOpts(width=2),
#         label_opts=opts.LabelOpts(is_show=False)
#     )
#
# # 渲染图表
# output_path = "pollutant_radar_chart.html"
# radar.render(output_path)
# webbrowser.open(output_path)


import pandas as pd
import webbrowser
from pyecharts.charts import Radar
from pyecharts import options as opts

# === 步骤 1：读取并整合 2020-2025 各年污染物数据 ===

file_path = "data/北京空气质量_2020到2025.xlsx"  # 替换为你的路径
sheet_names = ['2020', '2021', '2022', '2023', '2024', '2025']
pollutants = ['PM2.5', 'PM10', 'So2', 'No2', 'Co', 'O3']

combined_data = []

for year in sheet_names:
    df_year = pd.read_excel(file_path, sheet_name=year)
    df_year['year'] = int(year)
    df_year = df_year[pollutants + ['year']]
    combined_data.append(df_year)

df_all = pd.concat(combined_data, ignore_index=True)

# 每年平均污染物
yearly_avg = df_all.groupby('year')[pollutants].mean().reset_index()
year_labels = yearly_avg['year'].astype(str).tolist()

# 雷达图指标最大值设定
# schema = [
#     opts.RadarIndicatorItem(name=p, max_=max(df_all[p]) * 1.2)
#     for p in pollutants
# ]
schema = [
    opts.RadarIndicatorItem(name='PM2.5', max_=50),
    opts.RadarIndicatorItem(name='PM10', max_=100),
    opts.RadarIndicatorItem(name='So2', max_=10),
    opts.RadarIndicatorItem(name='No2', max_=50),
    opts.RadarIndicatorItem(name='Co', max_=1.0),
    opts.RadarIndicatorItem(name='O3', max_=100),
]


# 每年的数据准备好
radar_data = [yearly_avg[pollutants].iloc[i].tolist() for i in range(len(yearly_avg))]

# 构建雷达图对象
radar = (
    Radar()
    .add_schema(
        schema=schema,
        shape="polygon",
        radius="65%",
        textstyle_opts=opts.TextStyleOpts(font_size=12)
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(
            title="2020–2025年 北京主要污染物年均浓度雷达图",
            pos_left='center'
        ),
        legend_opts=opts.LegendOpts(
            type_='scroll',
            pos_bottom='5%'
        ),
        toolbox_opts=opts.ToolboxOpts()
    )
)

# 正确添加每一年曲线（每年一个 series_name，图例才会出现）
colors = ["#5470C6", "#91CC75", "#FAC858", "#EE6666", "#73C0DE", "#3BA272"]

for i, year in enumerate(year_labels):
    radar.add(
        series_name=year,
        data=[radar_data[i]],
        linestyle_opts=opts.LineStyleOpts(width=2, color=colors[i], opacity=0.6),
        label_opts=opts.LabelOpts(is_show=False)
    )

# 渲染图表
output_path = "pollutant_radar_chart.html"
radar.render(output_path)
webbrowser.open(output_path)
